<template>
    <div class="app-container calendar-list-container" v-loading="loading" element-loading-text="正在导出数据">
    <!-- filter 条件查询 -->
    <div class="btn-add">
        <el-button
        icon="el-icon-circle-plus-outline"
        type="button"
        class="el-button el-button--primary"
        @click="clickAdd"
        >
            新增</el-button
        >
    </div>

    <el-dialog title="新增" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" label-width="100px">
        <el-form-item label="工装条码">
          <el-input v-model="form.facilityBarcode"  placeholder="请选择条码" autocomplete="off" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="校验类别">
          <el-select v-model="form.verifType" placeholder="请选择类别">
            <el-option v-for="(item,index) in verifTypeList" :key="index" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submlt">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 修改数据弹窗 -->
    <el-dialog
      :title="title"
      :visible.sync="showAdd"
      @close="closeShow"
      width="60%"
    >
        <el-form :model="createForm" ref="createForm" :rules="addRules" label-width="80px">
            <el-row>    
                <el-col :span="8">
                    <el-form-item label="编号" prop="facilityBarcode" >
                        <el-input disabled v-model="createForm.facilityBarcode" class="ul-item"/>
                    </el-form-item>
                    <el-form-item label="名称" prop="facilityName" >
                        <el-input disabled v-model="createForm.facilityName" class="ul-item"/>
                    </el-form-item>
                    <el-form-item label="部门" prop="workCenter" >
                        <el-select disabled  clearable filterable v-model="createForm.workCenter" placeholder="请选择部门" @focus="getCenterList" class="ul-item">
                          <el-option :value="item" :label="item" v-for="(item,index) in this.centerList.data" :key="index"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="大类" prop="facilityBarcodeType1" >
                        <el-select
                        disabled
                       v-model="createForm.facilityBarcodeType1"
                       class="ul-item"
                       @focus="getBarcodeType('大类')">
                       <el-option
                        v-for="(item,index) in barcodeType1List"
                        :key="index"
                        :label="item.facilityBarcodeName"
                        :value="item.facilityBarcodeName">
                    </el-option>
                </el-select>
                        <!-- <el-input v-model="createForm.facilityBarcodeType1" class="ul-item"/> -->
                    </el-form-item>
                    <el-form-item label="型号" prop="facilityBarcodeType3" >
                        <el-select
                        disabled
                     filterable
                    v-model="createForm.facilityBarcodeType3"
                    class="ul-item"
                    @focus="getBarcodeType('规格')">
                    <el-option
                        v-for="(item,index) in barcodeType3List"
                        :key="index"
                        :label="item.facilityBarcodeName"
                        :value="item.facilityBarcodeName">
                    </el-option>
                </el-select>
                        <!-- <el-input v-model="createForm.facilityBarcodeType3" class="ul-item"/> -->
                    </el-form-item>
                    <el-form-item label="校验类别" prop="verifType" >
                        <el-select
                        disabled
                            filterable
                            v-model="createForm.verifType"
                            class="ul-item">
                            <el-option
                                v-for="(item,index) in verifTypeList"
                                :key="index"
                                :label="item"
                                :value="item">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="小类" prop="facilityBarcodeType2" >
                        <el-select disabled filterable v-model="createForm.facilityBarcodeType2" class="ul-item" @focus="getBarcodeType('小类')">
                            <el-option v-for="(item,index) in barcodeType2List" :key="index" :label="item.facilityBarcodeName" :value="item.facilityBarcodeName"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="位置" prop="warehouseLocation" >
                        <el-select disabled filterable v-model="createForm.warehouseLocation" class="ul-item">
                            <el-option v-for="(item,index) in placeList" :key="index" :label="item" :value="item"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="日期" prop="verifTime" >
                      <el-date-picker disabled clearable v-model="createForm.verifTime" value-format="yyyy-MM-dd HH:mm:ss"
                      format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择日期时间" class="ul-item" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row v-if="createForm.verifType == '内校'">    
                <el-col :span="4">
                    <el-form-item label="类别" prop="type" > 
                        <el-select style="width: 100px;" filterable  v-model="createForm.type" @change="typeChange" class="ul-item">
                            <el-option v-for="(item,index) in typeList" :key="index" :label="item" :value="item"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="测量值1" v-if="createForm.type != '尺寸' && createForm.type != '尺寸' && createForm.access == '手动填写' && [1,2,3,4,5,6,7,8,9,10].includes(createForm.testCount)" prop="momentTestValue1" >
                        <el-input style="width: 100px;" v-model="createForm.momentTestValue1" class="ul-item"/>
                    </el-form-item>
                    <el-form-item label="测量值6" v-if="createForm.type != '尺寸' && createForm.access == '手动填写' && [6,7,8,9,10].includes(createForm.testCount)" prop="momentTestValue6" >
                        <el-input style="width: 100px;" v-model="createForm.momentTestValue6" class="ul-item"/>
                    </el-form-item>
                    <el-form-item label="平均值" v-if="createForm.access == '手动填写'" prop="momentTestValue" >
                        <el-input v-model="createForm.momentTestValue" class="ul-item" disabled/>
                    </el-form-item>
                </el-col>
                <el-col :span="5">
                    <el-form-item v-if="createForm.type != '尺寸'" label="标准值" prop="standardValueOfTorque" >
                        <el-input v-model="createForm.standardValueOfTorque" class="ul-item"/>
                    </el-form-item>
                    <el-form-item label="测量值2" v-if="createForm.type != '尺寸' && createForm.access == '手动填写' && [2,3,4,5,6,7,8,9,10].includes(createForm.testCount)" prop="momentTestValue2" >
                        <el-input v-model="createForm.momentTestValue2" class="ul-item"/>
                    </el-form-item>
                    <el-form-item label="测量值7" v-if="createForm.type != '尺寸' && createForm.access == '手动填写' && [7,8,9,10].includes(createForm.testCount)" prop="momentTestValue7" >
                        <el-input v-model="createForm.momentTestValue7" class="ul-item"/>
                    </el-form-item>
                    <el-form-item label="获取方式" prop="access" >
                        <el-select filterable  v-model="createForm.access" class="ul-item">
                            <el-option v-for="(item,index) in accessList" :key="index" :label="item" :value="item"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="5">
                    <el-form-item label="单位" prop="unit" >
                        <el-select clearable  filterable  v-model="createForm.unit" class="ul-item">
                            <el-option v-for="(item,index) in unitList" :key="index" :label="item" :value="item"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="测量值3" v-if="createForm.type != '尺寸' && createForm.access == '手动填写' && [3,4,5,6,7,8,9,10].includes(createForm.testCount)" prop="momentTestValue3" >
                        <el-input v-model="createForm.momentTestValue3" class="ul-item"/>
                    </el-form-item>
                    <el-form-item label="测量值8" v-if="createForm.type != '尺寸' && createForm.access == '手动填写' && [8,9,10].includes(createForm.testCount)" prop="momentTestValue8" >
                        <el-input v-model="createForm.momentTestValue8" class="ul-item"/>
                    </el-form-item>
                    <el-form-item label="标准差值" v-if="createForm.access == '手动填写'" prop="standardDeviationValue" >
                        <el-input v-model="createForm.standardDeviationValue" class="ul-item" disabled/>
                    </el-form-item>
                </el-col>
                <el-col :span="5">
                    <el-form-item label="精度" prop="precision1" >
                        <!-- <el-input v-model="createForm.precision1" class="ul-item"/> -->
                        <el-select filterable clearable v-model="createForm.precision1" class="ul-item">
                            <el-option v-for="(item,index) in precision1List" :key="index" :label="item.name" :value="item.name"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="测量值4" v-if="createForm.type != '尺寸' && createForm.access == '手动填写' && [4,5,6,7,8,9,10].includes(createForm.testCount)" prop="momentTestValue4" >
                        <el-input v-model="createForm.momentTestValue4" class="ul-item"/>
                    </el-form-item>
                    <el-form-item label="测量值9" v-if="createForm.type != '尺寸' && createForm.access == '手动填写' && [9,10].includes(createForm.testCount)" prop="momentTestValue9" >
                        <el-input v-model="createForm.momentTestValue9" class="ul-item"/>
                    </el-form-item>
                    <el-form-item label="校验结果" v-if="createForm.access == '手动填写'" prop="isAccomplish" >
                        <el-input v-model="createForm.isAccomplish" class="ul-item" disabled/>
                    </el-form-item>
                </el-col>
                <el-col :span="5">
                    <el-form-item label="基础数据" prop="testCount" >
                        <el-select filterable  v-model="createForm.testCount" @change="testChange" class="ul-item">
                            <el-option v-for="(item,index) in testCountList" :key="index" :label="item" :value="item"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="测量值5" v-if="createForm.type != '尺寸' && createForm.access == '手动填写' && [5,6,7,8,9,10].includes(createForm.testCount)" prop="momentTestValue5" >
                        <el-input v-model="createForm.momentTestValue5" class="ul-item"/>
                    </el-form-item> 
                    <el-form-item label="测量值0" v-if="createForm.type != '尺寸' && createForm.access == '手动填写' && [10].includes(createForm.testCount)" prop="momentTestValue10" >
                        <el-input v-model="createForm.momentTestValue10" class="ul-item"/>
                    </el-form-item>
                </el-col>
            </el-row>
            <div v-if="createForm.verifType == '内校' && createForm.type == '尺寸'">
                <div style="display: flex;">
                    <el-form-item label="长测量值">
                        <el-input type="number" v-model="sizeObj.length" class="ul-item"/>
                    </el-form-item>
                    <el-form-item label="宽测量值">
                        <el-input type="number" v-model="sizeObj.wide" class="ul-item"/>
                    </el-form-item>
                    <el-form-item label="高测量值">
                        <el-input type="number" v-model="sizeObj.height" class="ul-item"/>
                    </el-form-item>
                </div>
                <el-link :underline="false" type="danger">注意：基础数据维护多少个，以下标准值维护多少组</el-link>
                <el-table  :data="sizeList" stripe border fit highlight-current-row style="width: 100%;margin-top:15px;">
                  <el-table-column  label="序号" type="index" align="center" width="80px"/>
                  <el-table-column label="长测量值"  prop="length" align="center">
                    <template slot-scope="scope">
                        长测量值 {{ scope.$index+1 }} <el-input v-model="scope.row.length" type="number" style="width: 100px;"></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column label="宽测量值"  prop="wide" align="center">
                    <template slot-scope="scope">
                        宽测量值 {{ scope.$index+1 }} <el-input v-model="scope.row.wide" type="number" style="width: 100px;"></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column label="高测量值"  prop="height" align="center">
                    <template slot-scope="scope">
                        高测量值 {{ scope.$index+1 }} <el-input v-model="scope.row.height" type="number" style="width: 100px;"></el-input>
                    </template>
                  </el-table-column>
                </el-table>
            </div>
            <el-form-item label="外校证书" v-if="createForm.verifType == '外校'" prop="certificate" label-width="80px" >
                {{ createForm.certificate }}
                <el-upload
                class="avatar-uploader"
                action="http://192.168.3.11:8086/technologyFile"
                :on-success="handlePdfSuccess"
                :on-remove = "removePdf"
                :on-change= "pdfRemove"
                :headers="getHttpHeader"
                :before-upload="beforeFilsUpload">
                <!-- object标签用于包含对象，比如图像、音频、视频、java applets、ActiveX、PDF以及Flash -->
                <el-button v-if="fileBool" size="small" type="primary">点击上传</el-button>
                </el-upload>
              </el-form-item>
        </el-form>

        <!-- 公共底部 -->
        <div slot="footer" class="dialog-footer">
            <el-button @click="showAdd = false">取 消</el-button>
            <el-button
            type="primary"
            @click="addfacility('createForm')"
            >确定</el-button
            >
        </div>
    </el-dialog>


    <div class="filter-container">
      <!-- style="width: 150px;margin-right:15px;" -->
        <el-input
            @keyup.enter.native="getData"
            clearable
            style="margin-right: 15px"
            :style="{ width: inputWid(param.facilityBarcode) }"
            v-model="param.facilityBarcode"
            placeholder="条码"
        ></el-input>

        <el-select
            style="margin-right: 15px"
            :style="{ width: inputWid(param.facilityBarcodeType1) }"
            v-model="param.facilityBarcodeType1"
            placeholder="资源大类"
            class="ul-item"
            filterable
            clearable
            @focus="getBarcodeType('大类')">
            <el-option
                v-for="(item,index) in barcodeType1List"
                :key="index"
                :label="item.facilityBarcodeChineseName"
                :value="item.facilityBarcodeName">
            </el-option>
        </el-select>

        <el-select
            style="margin-right: 15px;width: 150px"
            v-model="param.facilityBarcodeType2"
            placeholder="资源小类"
            class="ul-item"
            filterable
            clearable
            @focus="getBarcodeType('小类')">
            <el-option
                v-for="(item,index) in barcodeType2List"
                :key="index"
                :label="item.facilityBarcodeChineseName"
                :value="item.facilityBarcodeName">
            </el-option>
        </el-select>

        <el-select
            style="margin-right: 15px;width: 150px"
            v-model="param.facilityBarcodeType3"
            placeholder="规格型号"
            class="ul-item"
            filterable
            clearable
            @focus="getBarcodeType('规格')">
            <el-option
                v-for="(item,index) in barcodeType3List"
                :key="index"
                :label="item.facilityBarcodeChineseName"
                :value="item.facilityBarcodeName">
            </el-option>
        </el-select>

        <el-input
            @keyup.enter.native="getData"
            clearable
            style="width: 130px;margin-right:10px;"
            v-model="param.type"
            placeholder="类别"
        ></el-input>

        <el-input
            @keyup.enter.native="getData"
            clearable
            style="width: 130px;margin-right:10px;"
            v-model="param.workCenter"
            placeholder="工作中心"
        ></el-input>

        <el-date-picker
          v-model="dateOptions"
          type="datetimerange"
          value-format="yyyy-MM-dd HH:mm:ss"
          format="yyyy-MM-dd HH:mm:ss"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          style="margin-right:10px;">
        </el-date-picker>

        <!-- <el-input
            @keyup.enter.native="getData"
            clearable
            style="width: 150px;margin-right:15px;"
            v-model="param.facilityProcess"
            placeholder="使用工序"
        ></el-input> -->

        <el-button type="primary" v-waves icon="el-icon-search" @click="getData">搜索</el-button>
        <el-button
            type="button"
            v-waves
            class="el-button el-button--warning"
            @click="cancelSubmit"
        >
        重置</el-button>

    </div>

    <!-- table 表格 -->
    <el-table  :data="tableInfo" v-loading="loadingList" element-loading-text="给我一首歌的时间" stripe border fit highlight-current-row style="width: 100%;margin-top:15px;">
      <el-table-column  label="序号" type="index" align="center" width="80px"/>
      <el-table-column label="条码"  prop="facilityBarcode" align="center"/>
      <el-table-column label="大类"  prop="facilityBarcodeType1" align="center"/>
      <el-table-column label="小类"  prop="facilityBarcodeType2" align="center"/>
      <el-table-column label="名称"  prop="facilityName" align="center"/>
      <el-table-column label="规格型号"  prop="facilityBarcodeType3" align="center"/>
      <el-table-column label="工作中心"  prop="workCenter" align="center"/>
      <el-table-column label="存放位置"  prop="warehouseLocation" align="center"/>
      <el-table-column label="校验人员"  prop="spotJobNumber" align="center">
        <template slot-scope="scope" >
            {{ scope.row.spotJobNumber ? scope.row.spotJobNumber : scope.row.maintenanceJobNumber }}
            </template>
      </el-table-column>
      <el-table-column label="类别"  prop="type" align="center"/>
      <el-table-column label="校验类别"  prop="verifType" align="center"/>
      <el-table-column label="校验周期"  prop="createTime" align="center"/>
      <el-table-column label="是否成功"  prop="isAccomplish" align="center">
        </el-table-column>
        <el-table-column label="校验时间"  prop="verifTime" align="center"/>
      <el-table-column label="操作" fixed="right" align="center" width="160" v-if="userInfo.roleId == 0 || userInfo.roleId == 14 || userInfo.roleId == 27 || userInfo.roleId == 9 || userInfo.roleId == 36 || userInfo.roleId == 39">
            <template slot-scope="scope" >
                <div class="fx" style="display: flex">
                    <el-button size="mini" type="primary" @click="handleEdit(scope.row)">修改</el-button>
                    <el-button
                        icon="el-icon-delete"
                        class="el-button el-button--danger"
                        style="font-size: 15px;"
                        @click="handleDelete(scope.row,scope.$index)"
                    ></el-button>
                </div>
            </template>
      </el-table-column>
    </el-table>

   <!-- pagination 分页 -->
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="listQuery.page"
        :page-sizes="[5,10, 20, 30, 40, 100, 300, 500]"
        :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

    <el-divider></el-divider>

    <el-button style="margin-right: 10px" type="success" size="small" @click="hExportExcel">导出数据Excel</el-button>

  </div>
</template>
    
<script>    
import { selfacility,verifTestSel,verifTestAdd,verifTestUpd,updprocessorder,verifTestDel,getProcessList,selbarcodeType,facilityVerifTest } from "@/api/processRoute"
import { getCenter } from '../../api/workSection.js'
export default {
    name: 'unitCheck',
    data () {
        return {
            dialogFormVisible: false,
            title: '',
            listQuery: {
                page: 1,                                    //页码
                limit: 10                                   //每页多少条记录
            },
            param: {
                facilityTechnologyNo: '',                      //产品编码
                facilityProcess: '',                           //使用工序
            },
            total: 0,                                      //数据的总数
            centerList: {data:[]}, 
            tableInfo: [],
            form:{},
            loadingList: false,
            loading: false,                        //工作中心列表
            createForm: {},
            updForm: {},
            props: { multiple: true },
            optionsprocess: [],
            dateOptions: this.getNowTime(),
            pickerOptions: {                            // 选择时间范围
              shortcuts: [{
                text: '最近一周',
                onClick(picker) {
                  const end = new Date();
                  const start = new Date();
                  start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                  picker.$emit('pick', [start, end]);
                }
              }, {
                text: '最近一个月',
                onClick(picker) {
                  const end = new Date();
                  const start = new Date();
                  start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                  picker.$emit('pick', [start, end]);
                }
              }, {
                text: '最近三个月',
                onClick(picker) {
                  const end = new Date();
                  const start = new Date();
                  start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                  picker.$emit('pick', [start, end]);
                }
              }]
            },
            // 表单校验
            addRules: {
                facilityBarcode: [
                    { required: true, message: "编号不能为空!", trigger: "change" },
                ],
                facilityName: [
                    { required: true, message: "名称不能为空!", trigger: "change" },
                ],
                workCenter: [
                    { required: true, message: "部门不能为空!", trigger: "change" },
                ],
                facilityBarcodeType1: [
                    { required: true, message: "大类不能为空!", trigger: "change" },
                ],
                facilityBarcodeType3: [
                    { required: true, message: "型号不能为空!", trigger: "change" },
                ],
                type: [
                    { required: true, message: "类别不能为空!", trigger: "change" },
                ],
                facilityBarcodeType2: [
                    { required: true, message: "小类不能为空!", trigger: "change" },
                ],
                warehouseLocation: [
                    { required: true, message: "位置不能为空!", trigger: "change" },
                ],
                verifTime: [
                    { required: true, message: "日期不能为空!", trigger: "change" },
                ],
                verifType: [
                    { required: true, message: "校验类别不能为空!", trigger: "change" },
                ],
                momentTestValue1: [
                    { required: true, message: "标准值1不能为空!", trigger: "change" },
                ],
                momentTestValue6: [
                    { required: true, message: "标准值6不能为空!", trigger: "change" },
                ],
                access: [
                    { required: true, message: "获取方式不能为空!", trigger: "change" },
                ],
                standardValueOfTorque: [
                    { required: true, message: "标准值不能为空!", trigger: "change" },
                ],
                momentTestValue2: [
                    { required: true, message: "标准值2不能为空!", trigger: "change" },
                ],
                momentTestValue7: [
                    { required: true, message: "标准值7不能为空!", trigger: "change" },
                ],
                // momentTestValue: [
                //     { required: true, message: "平均值不能为空!", trigger: "change" },
                // ],
                unit: [
                    { required: true, message: "单位不能为空!", trigger: "change" },
                ],
                momentTestValue3: [
                    { required: true, message: "标准值3不能为空!", trigger: "change" },
                ],
                momentTestValue8: [
                    { required: true, message: "标准值8不能为空!", trigger: "change" },
                ],
                // standardDeviationValue: [
                //     { required: true, message: "标准差值不能为空!", trigger: "change" },
                // ],
                precision1: [
                    { required: true, message: "精度不能为空!", trigger: "change" },
                ],
                momentTestValue4: [
                    { required: true, message: "标准值4不能为空!", trigger: "change" },
                ],
                momentTestValue9: [
                    { required: true, message: "标准值9不能为空!", trigger: "change" },
                ],
                // isAccomplish: [
                //     { required: true, message: "校验结果不能为空!", trigger: "change" },
                // ],
                testCount: [
                    { required: true, message: "基础数据不能为空!", trigger: "change" },
                ],
                momentTestValue5: [
                    { required: true, message: "标准值5不能为空!", trigger: "change" },
                ],
                momentTestValue10: [
                    { required: true, message: "标准值10不能为空!", trigger: "change" },
                ],
                certificate: [
                    { required: true, message: "外校证书不能为空!", trigger: "change" },
                ],
            },
            getHttpHeader: {                    //上传文件的头
                Authorization: '',              //传输文件的token
                CHECK_USER_VALUE: 'GOLWAY',
                CHECK_PASSWORD_VALUE: '8D4DD3F80CEB6149329FDBD590B164F1'
            },
            fileBool: true,         // 上传文件控制按钮
            showAdd: false,
            showupd: false,
            userInfo: this.$store.state.user.userInfo,
            barcodeType1List: [],
            barcodeType2List: [],
            barcodeType3List: [],
            // 类型列表
            typeList:['力矩','尺寸','气压','重量','电压','电流','电阻','温度','拉力','时间','漏电流','圈数','功率','声压','频率','调整率','功率因数','电平','电容','电感','亮度','色温','光泽度','色度','转速','角度','速度','硬度','浓度'],
            // 类别
            verifTypeList:['内校','外校'],
            // 单位
            unitList:['kgf.cm','N.m','mm','m','Mpa','kg','A','mA','V','Ω','mΩ','℃','℉','N','KN','nS','mS','S','圈','w','kw','Db','Hz','KHz','%','pF','uF','cd/m2','r/min','°','HRA','HRB','HRAC','g/m3'],
            // 基础数据
            testCountList: [0,3,4,5,6,7,8,9,10],
            // 精度
            precision1List: [{name:'±0.1%',id:0.001},{name:'±0.5%',id:0.005},{name:'±1%',id:0.01},{name:'±3%',id:0.03},{name:'±5%',id:0.05},{name:'±10%',id:0.1}],
             // 库房位置列表
            placeList: ['EMS工具房','EMS装备库','数控模具库','数控工装检具库','焊接工装检具库','喷涂工装检具库','装配工装库','装配检具库','研发实验室','钣金质量部','数控车间','焊接车间','喷涂车间','装配车间','医疗车间','物流部'],
            //  获取方式
             accessList:['自动获取','手动填写'],
            // 先择尺寸的列表
             sizeList:[],
             sizeObj:{
                length:'',
                wide:'',
                height:'',
             }
        }
    },
    computed: { 
      inputWid() {  
        return function (value) {
            if(!value || (value.length <= 10)){
              return '150px'
            } else {
              return (150 + (String(value).length-10)*6) + 'px'
            }
        }
      }
    },
    methods: {
        closeShow(){
            this.sizeObj = {length:'', wide:'', height:'', }
            this.sizeList = []
            this.$refs.createForm.resetFields();
            this.showAdd = false
        },
        // 创建一个函数，用于生成指定长度的对象数组  
         createObjectArray(length) {  
            // 使用 Array.from 方法生成一个指定长度的数组  
            // 然后在 map 方法中填充对象（这里只是示例对象，你可以根据需要自定义对象）  
            return Array.from({ length }, (_, index) => ({ length:'',wide:'',height:'' }));  
        },
        // 基础数据
        testChange(e){
            this.sizeList = []
            // if(this.createForm.type == '尺寸'){
                this.sizeList = this.createObjectArray(e)
            // }
        },
        // 类型改变
        typeChange(e){
            if(e == '尺寸'){
                this.createForm.access = '手动填写'
                this.createForm.unit = 'mm'
                this.createForm.testCount = 0
            }
        },
        // 文件上传之前的函数，将token传入头中
        beforeFilsUpload(file) {
            this.getHttpHeader.Authorization = this.dealCookie()
        },
         // cookies的处理函数
         dealCookie() {
            const strcookie = document.cookie                     //获取cookie字符串
            const arrcookie = strcookie.split("; ")               //分割
            for ( var i = 0; i < arrcookie.length; i++) {
                const arr = arrcookie[i].split("=")
                if (arr[0] == "agent_Authorization") {
                    return arr[1]
                }
            }
            return ""
        },
        // 上传PDF文档成功回调函数
       handlePdfSuccess(response) {
            this.createForm.certificate = response.results
        },
         // 移除pdf
         async removePdf(file) {
            this.fileBool = true
            return this.createForm.certificate = ''
            // const {data} = await deleteFiles({image: this.form.pdfUrl})
            // this.form.pdfUrl = ''
            // console.log(file,'removePdf')
        },
        pdfRemove(e){
            if(e.status == 'fail') return this.$message.error("文件上传失败!")
            else if(e.status == 'success'){ 
                this.fileBool = false
                return this.$message.success("文件上传成功!")
            }
        },
        async submlt(){
            if(!this.form.facilityBarcode) return this.$message.error('请填写条码')
            if(!this.form.verifType) return this.$message.error('请填写校验类别')
            let query = {
                page:1,
                limit:10,
                facilityBarcode: this.form.facilityBarcode
            }
            let name
            const res = await selfacility(query)
            if(res.data.records.length){
                name = res.data.records[0].userName
            } else {
                return this.$message.error('请输入正确条码!')
            }
            let query1 = {
                verifType: this.form.verifType,
                facilityBarcode: this.form.facilityBarcode,
                userName: name
            }
            const res1 = await verifTestAdd(query1)
            console.log(res1)
            if(res1.code == '500') return
            if(res1.code != '1') return this.$message.error(errmsg)
            this.$message.success(res1.msg)
            this.dialogFormVisible = false
            this.handleFilter()
        },
        // 获取工作中心
      async getCenterList() {
        let query = {}
        const data = await getCenter(query)
        this.centerList = data
      },
        clickAdd(){
            this.dialogFormVisible = true
        },
        getData(){  
          this.listQuery.page = 1
          this.listQuery.limit = 10
          this.handleFilter()
        },
        async handleFilter() {
            this.loadingList = true
            let query = Object.assign({},this.listQuery,this.param)
            if(this.dateOptions) {
              if(this.dateOptions.length) {
                  query.strTime = this.dateOptions[0]
                  query.endTime = this.dateOptions[1]
              }
            }
            let data = await verifTestSel(query)
            let errmsg = data.msg
            this.loadingList = false
            if(data.code == '500') return
            if(data.code != '1') return this.$message.error(errmsg)
            this.total = data.data.count
            this.tableInfo = data.data.list
        },
        async getProcessList(technologyNo){
            this.optionsprocess = []
            this.updForm.facilityProcess = ''
            this.createForm.facilityProcess = ''
            if(!technologyNo) return
            let data = await getProcessList({technologyNo: technologyNo,technologyEnd: technologyNo})
            let errmsg = data.msg
            if(data.code == '500') return
            if(data.code != '1') return this.$message.error(errmsg)
            data.data.records.forEach(item=>{
                let obj = {}
                obj.value = item.processNo
                obj.label = item.processNo
                this.optionsprocess.push(obj)
            })
        },
        //初始默认执行(重点)，返回日期格式YYYY-MM-DD HH:MM:SS
        getNowTime() {
        let dateTime = +new Date()+8*3600*1000
        const start = new Date(dateTime - 3600 * 1000 * 24 * 7)
            .toISOString()
            .replace('T', ' ')
            .split('.')[0]
            // .substring(0,10) //默认开始时间7天前
        const end = new Date(dateTime)
            .toISOString()
            .replace('T', ' ')
            .split('.')[0]
            // .substring(0,10)//默认结束时间当前时间
        console.log([start, end])
        return [start, end]
        },
        // table表单下方的页码组件改变每页显示的数据条数
        handleSizeChange(val) {
          this.listQuery.limit = val
          this.handleFilter()
        },
        // table表单下方的页码组件改变页面
        handleCurrentChange(val) {
          this.listQuery.page = val
          this.handleFilter()
        },
        transformDat(res) {
          const mapInfo = {
            index: '序号',
            facilityBarcode: '条码',
            facilityBarcodeType1: '大类',
            facilityBarcodeType2: '小类',
            facilityName: '名称',
            facilityBarcodeType3: '规格型号',
            workCenter: '工作中心',
            warehouseLocation: '存放位置',
            spotJobNumber: '校验人员',
            type: '类别',
            verifType: '校验类别',
            verifTime: '校验周期',
            isAccomplish: '是否成功',
            updateTime: '校验时间',
          }
          // 对data进行格式化
          // 取出所有的属性名
          const props = Object.keys(res[0]) // ['id','']
          // 将属性名从英文转为中文
          const header = props.map(zhkey => mapInfo[zhkey])
          console.log('中文的表头', header)
          // const data=[['xxxx','总裁办'，'13800000']]
          const data = res.map(obj => Object.values(obj))
          return { header, data }
        },
        // 导出excel
        async hExportExcel() {
          this.loading = true
          let query = Object.assign({},this.listQuery,this.param)
          query.page = 1
          query.limit = 10000
          if(this.dateOptions) {
              if(this.dateOptions.length) {
                  query.strTime = this.dateOptions[0]
                  query.endTime = this.dateOptions[1]
              }
            }
          let arr = await verifTestSel(query)
          if(arr.code == '500') return
          if(arr.code != '1'){
            this.loading = false
            return this.$message.error(errmsg)
          }
          if(!arr.data.list.length) return this.$message.error('暂无数据')
          const { header, data } = this.transformDat(this.setSort(arr.data.list))
          import('@/vendor/Export2Excel').then(excel => {
            // excel表示导入的模块对象
            console.log(excel)
            excel.export_json_to_excel({
            header, // 表头 必填
            data, // 具体数据 必填
            filename: '设备校验管理', // 文件名称
            autoWidth: true, // 宽度是否自适应
            bookType: 'xlsx' // 生成的文件类型
            })
          })
          this.loading = false
        },
        // 排序函数
        setSort(arr){
          let newArr = JSON.parse(JSON.stringify(arr))
          let arr1 = []
          newArr.forEach((item,index)=>{
            let obj = {
                index: index+1,
                facilityBarcode: item.facilityBarcode,
                facilityBarcodeType1: item.facilityBarcodeType1,
                facilityBarcodeType2: item.facilityBarcodeType2,
                facilityName: item.facilityName,
                facilityBarcodeType3: item.facilityBarcodeType3,
                workCenter: item.workCenter,
                warehouseLocation: item.warehouseLocation,
                spotJobNumber: item.spotJobNumber,
                type: item.type,
                verifType: item.verifType,
                verifTime: item.verifTime,
                isAccomplish: item.isAccomplish,
                updateTime: item.updateTime,
            }
            arr1.push(obj)
          })
          return arr1
        },
        cancelSubmit(){
            this.param = {
                facilityTechnologyNo: '',                      //产品编码
                facilityProcess: '',                                //使用工序
            }
            this.listQuery.page = 1
            this.handleFilter()
        },
        // 提交表单之前先进行验证
        addfacility(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.submitprocess()
                } else {
                    return false
                }
            })
        },
        async submitprocess(){
            let query = Object.assign({},this.createForm)
            if(query.type == '尺寸'){
                if(!this.sizeObj.length || !this.sizeObj.wide || !this.sizeObj.height) return this.$message.warning('请填写标准值长 宽 高！')
                for (const item of this.sizeList) {
                    if(!item.length || !item.wide || !item.height) return this.$message.warning('请把标准值长 宽 高填写完整！')
                }
            }
            if(query.precision1){
                let num = query.precision1.replace(/[%±]/g, '') // 去掉% 和 ±
                query.precision1 = Number(num)/100
            }
            if(query.verifType == '内校') { 
                delete query.certificate
            } else if(query.verifType == '外校'){
                delete query.type
                delete query.standardValueOfTorque
                delete query.unit
                delete query.precision1
                delete query.testCount
                delete query.access
                delete query.momentTestValue1
                delete query.momentTestValue2
                delete query.momentTestValue3
                delete query.momentTestValue4
                delete query.momentTestValue5
                delete query.momentTestValue6
                delete query.momentTestValue7
                delete query.momentTestValue8
                delete query.momentTestValue9
                delete query.momentTestValue10
            }
            // 类型为 尺寸 就传data
            let query1 = {
                size:[],
                standard_value_of_torque_length:null,
                standard_value_of_torque_wide:null,
                standard_value_of_torque_height:null,
                standard_scope_length:null,
                standard_scope_wide:null,
                standard_scope_height:null,
            }
            if(query.type == '尺寸'){
                query1 = {
                    size: this.sizeList,            
                    standard_value_of_torque_length: this.sizeObj.length,
                    standard_value_of_torque_wide: this.sizeObj.wide,
                    standard_value_of_torque_height: this.sizeObj.height,  
                    standard_scope_length: query.precision1,  
                    standard_scope_wide: query.precision1,  
                    standard_scope_height: query.precision1,  
                }
            }
            let data
            if(this.title == '新增'){
                data = await verifTestAdd(query)
            } else {
                delete query.createTime
                data = await verifTestUpd(query,query1)
            }
            const errMsg = data.msg
            if(data.code == '500') return
            if(data.code != '1') return this.$message.error(errMsg)
            this.$message.success(errMsg)
            this.resetForm()
            this.showAdd = false
            this.handleFilter()
        },
        resetForm(){
            this.sizeList = []
            this.sizeObj = { length:'',wide:'', height:'',}
            this.$refs.createForm.resetFields();
        },
        async handleEdit(row){
            console.log(row);
            this.createForm = Object.assign({},row)
            this.createForm.verifTime = this.setNowTime()
            const data = JSON.parse(row.data)
            if(data && this.createForm.type == '尺寸'){
                this.sizeObj.length = data.standard_value_of_torque_length
                this.sizeObj.wide = data.standard_value_of_torque_wide
                this.sizeObj.height = data.standard_value_of_torque_height
                this.sizeList = data.size
            }
            delete this.createForm.data
            // 获取之前提交的赋值
            const res = await facilityVerifTest({facilityBarcode:row.facilityBarcode})
            if(res.data){
                this.createForm.type = row.type ? row.type : res.data.type
                this.createForm.standardValueOfTorque = row.standardValueOfTorque ? row.standardValueOfTorque : res.data.standardValueOfTorque
                this.createForm.unit = row.unit ? row.unit : res.data.unit
                this.createForm.precision1 = row.precision1 ? row.precision1 : res.data.precision1
                this.createForm.testCount = row.testCount ? row.testCount : res.data.testCount
                this.createForm.access = row.access ? row.access : res.data.access
                if(!row.data && res.data.data){
                    const dataData = JSON.parse(res.data.data)
                    console.log('dataData',dataData)
                    this.sizeObj.length = dataData.standard_value_of_torque_length
                    this.sizeObj.wide = dataData.standard_value_of_torque_wide
                    this.sizeObj.height = dataData.standard_value_of_torque_height  
                }
            }
            if(this.createForm.precision1) this.createForm.precision1 = `±${this.createForm.precision1 * 100}%`
            this.title = '修改'
            this.showAdd = true
        },
        setNowTime() {
            let date = new Date()
            // 获取年、月、日、时、分、秒  
            const year = date.getFullYear();  
            const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份是从0开始的  
            const day = String(date.getDate()).padStart(2, '0');  
            const hour = String(date.getHours()).padStart(2, '0');  
            const minute = String(date.getMinutes()).padStart(2, '0');  
            const second = String(date.getSeconds()).padStart(2, '0');  
            return `${year}-${month}-${day} ${hour}:${minute}:${second}`;   
        },
        handleDelete(row,index) {
            this.$confirm('这将会永久删除该行数据，是否继续?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.delprocess(row,index)
            }).catch(() => {
                this.$message({
                  type: 'info',
                  message: '已取消删除'
                })
            })
        },
        async delprocess(row,index){
          let res = await verifTestDel({id: row.id})
          if(res.code == '500') return
          if(res.code != '1') return this.$message.error(res.msg)
          this.tableInfo.splice(index,1)
          this.$message.success(res.msg)
        },
        async getBarcodeType(type) {
            this.loadingList = true
            let query = {
                facilityBarcodeType: type,
                page: 1,
                limit: 10000
            }
            let data = await selbarcodeType(query)
            let errmsg = data.msg
            if(data.code == '500') return
            if(data.code != '1') return this.$message.error(errmsg)
            if(type == '大类'){
                this.barcodeType1List = data.data.records
            } else if(type == '小类') {
                this.barcodeType2List = data.data.records
            } else if(type == '规格') {
                this.barcodeType3List = data.data.records
            }
            this.loadingList = false
        },
    },
    // 进入页面获取table信息
    mounted() {
        this.handleFilter()
    }
}
</script>

<style>

.btn-add{
    margin-bottom: 28px;
}

.ul-item{
  width: 85%;
}
</style>
